package io.tec.cloud.algorithm.c03_v20220607;

public class Code02_DeleteGivenValue {

    public static Node<Integer> deleteGivenValue(Node<Integer> head, int value) {
        while (head != null) {
            if (head.data == value) {
                head = head.next;
            }
            break;
        }
        if (head == null) {
            return head;
        }
        Node<Integer> pre = head;
        Node<Integer> next = head.next;
        while (next != null) {
            if (next.data != value) {
                pre.next = next;
                pre = pre.next;
            }
            next = next.next;
        }
        return head;
    }

    public static Node<Integer> deleteGivenValue2(Node<Integer> head, int value) {
        while (head != null) {
            if (head.data == value) {
                head = head.next;
            }
            break;
        }
        Node<Integer> pre = head;
        Node<Integer> cur = head;
        while (cur != null) {
            if (cur.data == value) {
                pre.next = cur.next;
            } else {
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }

    public static Node<Integer> deleteGivenValue3(Node<Integer> head, int value) {
        while (head != null) {
            if (head.data == value) {
                head = head.next;
            }
            break;
        }

        Node pre = head;
        Node curr = head;
        while (curr != null) {
            if (curr.data == curr) {
                pre.next = curr.next;
            } else {
                pre = curr;
            }
            curr = curr.next;
        }
        return head;
    }
}
